package org.lzh.controller;

import org.apache.ibatis.annotations.Param;
import org.lzh.pojo.Problem;
import org.lzh.pojo.TestPaper;
import org.lzh.service.TestPaperAndProblemService;
import org.lzh.service.TestPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

//试卷管理类
@Controller
@RequestMapping("testPaper")
public class TestPaperController {
    @Autowired
    private TestPaperService testPaperService;

    @Autowired
    private TestPaperAndProblemService testPaperAndProblemService;

        //添加试卷的弹窗展示页面
        @RequestMapping(value="/showAddTestPaper")
        public String showAddTestPaper(){
            return "testPaper/addTestPaper";
        }


    //    ---------------------------------------------------------------新增操作-----------------------------------------------------------


        //添加问题
        @RequestMapping(value = "/addTestPaper", method = RequestMethod.POST)
        public String login(
                            @ModelAttribute TestPaper testPaper,//@ModelAttribute这个注解可以将注册时输入的值封装成user对象，很方便
                            Model model             //model可以用model.addAttribute 添加属性，然后在跳转的jsp页面里用，也很好用
        ) throws IOException {

            if(!testPaper.getId().isEmpty()&&!testPaper.getName().isEmpty()&&!testPaper.getRemark().isEmpty()){
                //判断是否已经存在了这个id
                if(findById(testPaper.getId())!=null){
                    model.addAttribute("id",testPaper.getId());
                    model.addAttribute("tip","学号已存在");
                    return "testPaper/addTestPaper" ;
                }
                testPaperService.insertTestPaper(testPaper);
                model.addAttribute("tip", "添加成功");
            }else{
                model.addAttribute("tip", "不能为空");
            }
            return "testPaper/addTestPaper" ;


        }




        //测试绑定问题
        @RequestMapping(value="/bindProblem", method = RequestMethod.POST)
        @ResponseBody
        public String bindProblem(String testPaperId ,String ids,Model model){
            return testPaperAndProblemService.bindProblem(testPaperId,ids);

        }


    //    ---------------------------------------------------------------删除操作-----------------------------------------------------------

        //删除一个试卷
        @RequestMapping(value = "/deleteTestPaper", method = RequestMethod.POST)
        public @ResponseBody String deleteUser(@Param("id")Integer id) {
            int a= testPaperService.deleteById(id);
            System.out.println(id);
            if(a > 0){
                return "OK";
            }else{
                return "FAIL";
            }
        }

    //根据ids批量删除试卷
    @RequestMapping(value = "/batchDeleteTestPaper", method = RequestMethod.POST)
    @ResponseBody
    public String batchdeleteById(@Param("ids")String ids){
        for(String id:ids.split(",")){
            Integer id1= Integer.valueOf(id);
            testPaperService.deleteById(id1);
            System.out.println("-------------------------------------------------------数据库删除成功");
        }
//        model.addAttribute("tip", "删除成功");
        return "删除成功了";
    }


    //测试解绑题目
    @RequestMapping(value = "/unbindProblem", method = RequestMethod.POST)
    @ResponseBody
    public String delete(String testPaperId, String ids){

        return testPaperAndProblemService.delete(testPaperId,ids);
    };

    //    ---------------------------------------------------------------查询操作-----------------------------------------------------------

        //根据id查找问题
        @RequestMapping(value = "/find")
        @ResponseBody
        public TestPaper findById(String id){
            return testPaperService.findById(id) ;
        }

        //查询出试卷列表，然后跳转到试卷管理页面  分页查询（通过数组）
        @RequestMapping(value="/testPaper/{currPage}/{pageSize}")
        public String user(Model model,
                           @PathVariable("currPage") int currPage,
                           @PathVariable("pageSize") int pageSize,
                           @ModelAttribute TestPaper testPaper){

            List<TestPaper> studentList1=testPaperService.findByDynamic1(testPaper);              //动态查询所有的数据,如果没有传user,则会查全部学生
            int length=studentList1.size();
            List<TestPaper> studentList =testPaperService.findByDynamic(testPaper,currPage,pageSize);         //把数据分页切割
            model.addAttribute("studentList",studentList);//分页后的学生数据
            model.addAttribute("length",length);//学生的全部数据的总条数
            int total=length/pageSize+(length%pageSize>0?1:0);            //总页数
            model.addAttribute("total",total);//学生的全部数据的总页数


        model.addAttribute("currPage",currPage);
        model.addAttribute("pageSize",pageSize);
//        model.addAttribute("tip","查询成功");
        model.addAttribute("testPaper",testPaper);

        return "testPaper/testPaper";
    }

        //根据id查找试卷,然后展示在更新操作的表上
        @RequestMapping(value = "/showUpdateProblem")
        public String findById(@Param("id")String id,Model model){
            TestPaper testPaper=testPaperService.findById(id);
            model.addAttribute("testPaper",testPaper);
            return "testPaper/updateTestPaper";
        }









    //测试通过试卷id查询这个试卷下的所有题目
    @RequestMapping(value = "/showAllProblem/{currPage}/{pageSize}")
    public String findAllProblem(String testPaperId,
                                        Model model,
                                        @PathVariable("currPage") int currPage,
                                        @PathVariable("pageSize") int pageSize
                                        ){
        List<Problem> studentList1=testPaperAndProblemService.findAllProblem(testPaperId);              //动态查询所有的数据,如果没有传user,则会查全部学生
        int length=studentList1.size();
        List<Problem> studentList =testPaperAndProblemService.findAllProblemPage(testPaperId,currPage,pageSize);         //把数据分页切割
        model.addAttribute("studentList",studentList);//分页后的学生数据
        model.addAttribute("length",length);//学生的全部数据的总条数
        int total=length/pageSize+(length%pageSize>0?1:0);            //总页数
        model.addAttribute("total",total);//学生的全部数据的总页数


        model.addAttribute("currPage",currPage);
        model.addAttribute("pageSize",pageSize);
//        model.addAttribute("tip","查询成功");
        model.addAttribute("testPaperId",testPaperId);

        return "testPaper/bindProblem/testPaperProblem";


    };



    //测试通过试卷id查询不属于这个试卷下的所有题目
    @RequestMapping(value = "/showNotAllProblem/{currPage}/{pageSize}")
    public String findAllNotProblem(String testPaperId,
                                           Model model,
                                           @PathVariable("currPage") int currPage,
                                           @PathVariable("pageSize") int pageSize){

        List<Problem> studentList1=testPaperAndProblemService.findAllNotProblem(testPaperId);              //动态查询所有的数据,如果没有传user,则会查全部学生
        int length=studentList1.size();
        List<Problem> studentList =testPaperAndProblemService.findAllNotProblemPage(testPaperId,currPage,pageSize);         //把数据分页切割
        model.addAttribute("studentList",studentList);//分页后的学生数据
        model.addAttribute("length",length);//学生的全部数据的总条数
        int total=length/pageSize+(length%pageSize>0?1:0);            //总页数
        model.addAttribute("total",total);//学生的全部数据的总页数


        model.addAttribute("currPage",currPage);
        model.addAttribute("pageSize",pageSize);
//        model.addAttribute("tip","查询成功");
        model.addAttribute("testPaperId",testPaperId);

        return "testPaper/bindProblem/bindProblem";

    };
    //    ---------------------------------------------------------------更新操作-----------------------------------------------------------
        //更新试卷信息
        @RequestMapping(value = "/updateTestPaper",method = RequestMethod.POST)
        public String updateById(
                                 @ModelAttribute TestPaper testPaper,                          //@ModelAttribute这个注解可以将注册时输入的值封装成user对象，很方便
                                 Model model    //model可以用model.addAttribute 添加属性，然后在跳转的jsp页面里用，也很好用
        ) throws IOException {
            System.out.println(testPaper.getId()+testPaper.getName()+testPaper.getRemark());

            if(!testPaper.getId().isEmpty()&&!testPaper.getName().isEmpty()&&testPaper.getRemark()!=null){
                testPaperService.updateById(testPaper);
                model.addAttribute("tip", "修改成功");
            }else{
                model.addAttribute("tip", "修改失败");
            }

            return "testPaper/updateTestPaper";
        }
}
